// pages/02admin/10goodsCate/goodsCate.js
var appData = getApp().globalData
var app = getApp()
Page({
    data: {
        goodsCateList: null
    },

    onLoad: function (options) {
        var that = this
        app.setNoticeGoodsCatePage(function (updateData) {
            var isHave = 0
            var list = that.data.goodsCateList
            for (var i = 0; i < list.length; i++) {
                if (updateData.cateId == list[i].cateId) {
                    list[i].name = updateData.name
                    list[i].nums = updateData.nums
                    isHave++
                }
            }
            if (!isHave) {
                list.push(updateData)
            }
            that.setData({
                goodsCateList: list
            })
            console.log(that.data.goodsCateList)
        })

        if (appData.goodsCateList) {
            this.setData({
                goodsCateList: appData.goodsCateList
            })
        } else {
            this.getGoodsCateData()
        }
    },

    getGoodsCateData: function () {
        var that = this
        var param = {
            uid: appData.shopInfo.shopInfo.userid,
            is_ad: 1
        }
        app.getJsonData('api/space/product_cate.php', param, function (goodsData) {
            that.handleGoodsData(goodsData)
        })
    },

    editGoods: function (e) {
        if (app.globalData.loginMode == 'boss') {
            let oneGoods;
            if (e.currentTarget.id != '') {
                oneGoods = JSON.stringify(this.data.goodsCateList[e.currentTarget.id]);
            }

            wx.navigateTo({
                url: '/pages/02admin/11setGoodsClass/goodsClass?goods=' + oneGoods
            })
        } else {
            app.toast('需要掌柜登录才能操作')
        }
    },

    delGoods: function (e) {
        if (app.globalData.loginMode == 'boss') {
            var i = e.currentTarget.id
            console.log('this.data==', this.data)
            var list = this.data.goodsCateList
            var that = this
            var param = {
                uid: appData.shopInfo.shopInfo.userid,
                sql: 'delete from',
                cate_id: list[i].cateId
            }
            app.getJsonData('api/admin/pro_cate_save.php', param, function (res) {
                if (res.code) {
                    that.toast(res.message)
                } else {
                    list.splice(i, 1)
                    that.setData({ goodsCateList: list })
                }
            })
        } else {
            this.toast('需要掌柜账号才能删除')
        }
    },

    toast: function (str) {
        wx.showToast({
            title: str,
            image: '/pages/images/qzg_logo_90x.jpg',
        })
    },

    handleGoodsData: function (goodsData) {
        if (goodsData.code == 0) {
            this.setData({
                goodsCateList: goodsData.data
            })
            appData.goodsCateList = goodsData.data
        }
        console.log(this.data.goodsData)
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function () {
        console.log('生命周期函数--监听页面初次渲染完成')
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        console.log('生命周期函数--监听页面显示')
        this.update()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log('生命周期函数--监听页面隐藏')
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        console.log('生命周期函数--监听页面卸载')
    },
})
